<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Term extends AppModel {
    var $_primary_key = TERM_TERM_ID;
    var $_tablename = TERM;
    var $_mapping = array(
                           array('id' => TERM_TERM_ID),
                           array('admin_id' => TERM_FRANCHISE_ID),
                           array('year' => TERM_YEAR),
                           array('the_term' => TERM_TERM),
                           array('theme' => TERM_THEME),
                           array('weeks' => TERM_WEEKS),
                           array('current' => TERM_CURRENT),
                           array('start_date' => TERM_START_DATE),
                           array('end_date' => TERM_END_DATE),
                           array('enrol_date' => TERM_ENROL_DATE),
                           array('title' => TERM_TITLE),
                           array('notes' => TERM_NOTES),
                            );
    public function Term(){
        parent::AppModel();
    }
    
    function get_tid(){
        $this->db->select('t.'.TERM_TERM_ID.' AS term_id');
        $this->db->from($this->_tablename.' t');
        $this->db->where('t.'.TERM_CURRENT, 'YES');
        $this->db->where('t.'.TERM_FRANCHISE_ID, $this->admin_id);
        $query = $this->db->get();
        if($query->num_rows() > 0){
            $row = $query->row();
            return $row->term_id;
        }else
            return  0;
    }
    
    function get_lists($limit = NULL, $offset = NULL, $order_by = NULL, $order_type=0){
        $conditions = array(TERM_FRANCHISE_ID => $this->admin_id);
        $this->db->where($conditions);
        $this->_numrow = $this->db->count_all_results($this->_tablename);
        if($this->_numrow > 0){
            $this->db->where($conditions);
            $this->db->limit($limit);
            $this->db->offset($offset);
            if(!is_null($order_by)){
                if(is_array($order_by)){
                    $s = '';
                    for($i=0;$i<count($order_by);$i++)
                        if($i != count($order_by)-1)
                            $s .= $this->sort_field($order_by[$i]).$this->sort_type($order_type).', ';
                        else
                            $s .= $this->sort_field($order_by[$i]).$this->sort_type($order_type);
                }else
                    $s = $this->sort_field($order_by).$this->sort_type($order_type);
                $this->db->order_by($s);
            }
            $query = $this->db->get($this->_tablename);
            if(empty($this->_mapping)){
                return $query->result();
            }else{
                $res = array();
                foreach($query->result() as $r){
                    foreach($this->_mapping as $map)
                        $r->{key($map)} = $r->{current($map)};
                    $res[] = $r;
                }
                return $res;
            }
        }
        return array();    
    }
    
    function save($data, $term_id = NULL){
        if($data[TERM_CURRENT]=='YES')
            $this->set_current_term();
        if(is_null($term_id)){
            $data[TERM_TERM_ID] = $this->get_last_id();
            $this->db->set($data);
            return $this->db->insert($this->_tablename);
        }else{
            $this->db->set($data);
            $this->db->where(TERM_TERM_ID, $term_id);
            $this->db->where(TERM_FRANCHISE_ID, $this->admin_id);
            return $this->db->update($this->_tablename);
        }
    }
    
    function get_last_id(){
        $this->db->select('MAX('.TERM_TERM_ID.') AS last_id');
        $this->db->where(TERM_FRANCHISE_ID, $this->admin_id);
        $result = $this->db->get($this->_tablename)->row();
        if(empty($result->last_id))
            return 1;
        else
            return $result->last_id + 1;
    }
    
    function set_current_term(){
        $this->db->set(TERM_CURRENT, 'NO');
        $this->db->where(TERM_FRANCHISE_ID, $this->admin_id);
        $this->db->update($this->_tablename);
        
        $this->db->set(TERM_CURRENT, 'YES');
        $this->db->where(TERM_FRANCHISE_ID, $this->admin_id);
        $this->db->where(TERM_TERM_ID, $this->get_id());
        $this->db->update($this->_tablename);
    }
    
    function get_list_by_fid(){
		return parent::get_lists(TERM_FRANCHISE_ID . ' = ' . $this->admin_id, null, null, TERM_YEAR);		
	}
}
?>